package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.AlarmManagement;
import com.example.prison.service.AlarmManagementService;
import com.example.prison.mapper.AlarmManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【alarm_management】的数据库操作Service实现
* @createDate 2023-05-29 11:14:59
*/
@Service
public class AlarmManagementServiceImpl extends ServiceImpl<AlarmManagementMapper, AlarmManagement>
    implements AlarmManagementService{

    @Autowired
    private AlarmManagementMapper alarmManagementMapper;

    @Override
    public PageInfo<AlarmManagement> get(int pageNumber, int pageSize, String alarmLevel) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(alarmManagementMapper.get(alarmLevel));
    }

    @Override
    public int add(AlarmManagement alarm) {
        String level = alarm.getAlarmLevel();
        if (ObjectUtils.isNotEmpty(alarmManagementMapper.getLevel(level))) {
            return -1;
        } else {
            alarmManagementMapper.add(alarm);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        alarmManagementMapper.deleteAlarm(ids);
    }

    @Override
    public int update(AlarmManagement alarm, Integer id) {
        AlarmManagement alarmDTO = alarmManagementMapper.getById(id);
        alarmManagementMapper.updateAlarm(alarm, id);
        List<AlarmManagement> alarmList = alarmManagementMapper.getLevel(alarm.getAlarmLevel());
        if(alarmList.size() > 1) {
            alarmManagementMapper.updateAlarmFail(alarmDTO, id);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public AlarmManagement getById(Integer id) {
        return alarmManagementMapper.getById(id);
    }

    @Override
    public AlarmManagement getOneLevel(String level) {
        return alarmManagementMapper.getOneLevel(level);
    }


}




